package com.gdut.huayan.entity.po;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.gdut.huayan.validation.NumRange;
import com.gdut.huayan.validation.group.GroupAdd;
import com.gdut.huayan.validation.group.GroupUpdate;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.time.LocalDateTime;
import java.util.List;

@Data
@TableName(value = "t_conversion_activity",autoResultMap = true)
public class ConversionActivity {

    @TableId(value = "id", type = IdType.AUTO)
    @Null(groups = {GroupAdd.class})
    @NotNull(groups = {GroupUpdate.class})
    private Long id;

    /**
     * 1、三皈 2、五戒
     */
    @NumRange(min = 1, max = 3, message = "type:1、三皈 2、五戒 3、三皈五戒")
    private Integer type;

    /**
     * 背景图片
     */
    private Long imageFileId;

    /**
     * 页面动态渲染json
     */
    @TableField(value = "page_json",typeHandler = JacksonTypeHandler.class)
    private List<JSONObject> pageJson;

    /**
     * 三皈活动开始时间
     */
    private LocalDateTime sanGuiStartTime;

    /**
     * 三皈活动结束时间
     */
    private LocalDateTime sanGuiEndTime;

    /**
     * 三皈皈依时间
     */
    private LocalDateTime sanGuiConversionTime;

    /**
     * 五戒活动开始时间
     */
    private LocalDateTime wuJieStartTime;

    /**
     * 五戒活动结束时间
     */
    private LocalDateTime wuJieEndTime;

    /**
     * 五戒皈依时间
     */
    private LocalDateTime wuJieConversionTime;

    /**
     * 辈分名
     */
    @NotBlank(message = "辈分名不能为空",groups = {GroupAdd.class})
    private String frontName;

    /**
     * 1-显示，0-隐藏
     */
    @NumRange(max = 1,min = 0,message = "status：1-显示，0-隐藏")
    private Integer status;


    public LocalDateTime getStartTimeByType(Integer appointType){
        if(type != 3){
            return type == 1 ? sanGuiStartTime : wuJieStartTime;
        }else if(appointType != null){
            return appointType == 1 ? sanGuiStartTime : wuJieStartTime;
        }
        return null;
    }

    public LocalDateTime getEndTimeByType(Integer appointType){
        if(type != 3){
            return type == 1 ? sanGuiEndTime : wuJieEndTime;
        }else if(appointType != null){
            return appointType == 1 ? sanGuiEndTime : wuJieEndTime;
        }
        return null;
    }

}
